package com.tencent.wxcloudrun.controller;

import com.tencent.wxcloudrun.controller.utils.ResultResponse;
import com.tencent.wxcloudrun.domain.Meal;
import com.tencent.wxcloudrun.service.MealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/meals")
public class MealController {

    @Autowired
    private MealService mealService;

    @GetMapping
    public ResultResponse getMeals() {
        return ResultResponse.ok(mealService.getAll(),"查询成功");
    }

    @PostMapping
    public ResultResponse addMeal(@RequestBody Meal meal) {
        boolean result = mealService.save(meal);
        return ResultResponse.ok(result,"新增成功");
    }

    @PutMapping
    public ResultResponse updateMeal(@RequestBody Meal meal) {
        boolean result = mealService.update(meal);
        return ResultResponse.ok(result,"更新成功");
    }

    @DeleteMapping("{id}")
    public ResultResponse deleteMeal(@PathVariable Long id) {
        boolean result = mealService.delete(id);
        return ResultResponse.ok(result,"删除成功");
    }

    @GetMapping("{id}")
    public ResultResponse getMealById(@PathVariable Long id) {
        return ResultResponse.ok(mealService.getById(id),"查询成功");
    }

    @GetMapping("{currentPage}/{pageSize}")
    public ResultResponse getMealByPage(@PathVariable Integer currentPage, @PathVariable Integer pageSize, Meal meal) {
        return ResultResponse.ok(mealService.getPage(currentPage,pageSize,meal),"查询成功");
    }
}
